<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2210 – This is a file redirection. Was it supposed to be a comparison or fd operation?</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2210 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2210">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="this-is-a-file-redirection-was-it-supposed-to-be-a-comparison-or-fd-operation">This
is a file redirection. Was it supposed to be a comparison or fd
operation?</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2210.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="ex">x</span> <span class="op">&gt;</span> 5<span class="kw">;</span> <span class="cf">then</span> <span class="bu">echo</span> <span class="st">&quot;true&quot;</span><span class="kw">;</span> <span class="cf">fi</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2210.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">foo</span> <span class="op">&gt;</span> /dev/null <span class="dv">2</span><span class="op">&gt;</span>1</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2210.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">((</span> <span class="va">x</span> <span class="op">&gt;</span> <span class="dv">5</span> <span class="kw">));</span> <span class="cf">then</span> <span class="bu">echo</span> <span class="st">&quot;true&quot;</span><span class="kw">;</span> <span class="cf">fi</span></span></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2210.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">foo</span> <span class="op">&gt;</span> /dev/null <span class="dv">2</span><span class="op">&gt;&amp;</span><span class="dv">1</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You are redirecting to or from a filename that is an integer. For
example, <code>ls &gt; file</code> where <code>file</code> happens to be
<code>3</code>.</p>
<p>This is not likely to be intentional. The most common causes are:</p>
<ol>
<li>Trying to compare two numbers, as in <code>x &gt; 5</code>. This
should instead be <code>[ "$x" -gt 5 ]</code> or
<code>(( x &gt; 5 ))</code>.</li>
<li>Trying similarly to compare command output, as in
<code>grep -c foo file &gt; 100</code> instead of
<code>[ "$(grep -c foo file)" -gt 100 ]</code></li>
<li>Malformed FD operations, such as writing <code>1&gt;2</code> instead
of <code>1&gt;&amp;2</code>.</li>
</ol>
<p>If you do want to create a file named <code>4</code>, you can quote
it to silence shellcheck and make it more clear to humans that it's not
supposed to be taken numerically.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If you use the <code>&amp;&gt;</code> form of redirection, as in
<code>foo &gt; /dev/null 2&gt;&amp;1</code>, it will trigger this
warning. You can safely ignore this warning if that is what triggered
it, or change your redirection operator to the semantically preferable
<code>&gt;&amp;</code>.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


